from maix import app, nn, camera, display, time

cam = camera.Camera(512, 320)   # Manually set resolution
                                # | 手动设置分辨率
disp = display.Display()        # MaixCAM default is 522x368
                                # | MaixCAM 默认是 522x368


speech = nn.Speech("/root/models/am_7332_192_int8.mud")
speech.init(nn.SpeechDevice.DEVICE_MIC)

kw_tbl = ['wo3 ni3 ma',
          'ni3 hao3',
          'zai4 ma']
kw_gate = [0.1, 0.1, 0.1]

def callback(data:list[float], len: int):
    for i in range(len):
        # print(f"\tkw{i}: {data[i]:.3f};", end=' ')
        if data[i] > 0.5:
            print("\n")
            print(f"触发关键词\tkw{i}")
    print("\n")

speech.kws(kw_tbl, kw_gate, callback, True)

while not app.need_exit():
    frames = speech.run(1)
    if frames < 1:
        print("run out\n")
        break

     # time.fps_start()          # Manually set fps calculation start point, comment here mean last time fps() call is start
                                # | 动设置帧率（FPS）计算开始点，这里注释了表示上一次 fps 函数即是开始
    img = cam.read()            # Get one frame from camera, img is maix.image.Image type object
                                # | 从摄像头获取一帧图像，img 是 maix.image.Image 类型的对象
    disp.show(img)              # Show image to screen
                                # | 将图像显示到屏幕
    fps = time.fps()            # Calculate FPS between last time fps() call and this time call.
                                # | 计算两次 fps 函数调用之间的帧率
    print(f"time: {1000/fps:.02f}ms, fps: {fps:.02f}") # print FPS in console
                                                       # | 在终端打印帧率（FPS）